package cn.tedu.evaluation.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.tedu.common.consts.data.ContentConsts;
import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.evaluation.management.dao.persist.repository.ICommentRepository;
import cn.tedu.evaluation.management.pojo.entity.Comment;
import cn.tedu.evaluation.management.pojo.param.CommentAddNewParam;
import cn.tedu.evaluation.management.pojo.param.CommentUpdateParam;
import cn.tedu.evaluation.management.pojo.vo.CommentVO;
import cn.tedu.evaluation.management.service.ICommentService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理评论数据的业务实现类
 *
 * @author java@傲绝
 * @version 3.0
 */
@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {
@Autowired
private ICommentRepository commentRepository;
    public CommentServiceImpl() {
        log.info("创建业务对象：CommentServiceImpl");
    }

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;


    @Override
    public void addNew(CurrentPrincipal currentPrincipal, CommentAddNewParam commentAddNewParam) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewParam,comment);
        comment.setUserId(currentPrincipal.getId());
        int rows = commentRepository.insert(comment);
        if (rows !=1){
            String message = "发布评论失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        int i = commentRepository.deleteById(id);
        if (i != 1) {
            String message = "删除评论失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, CommentUpdateParam commentUpdateParam) {
        log.debug("开始处理【修改类别详情】的业务，ID：{}，新数据：{}", id, commentUpdateParam);




        Comment comment = new Comment();
        BeanUtils.copyProperties(commentUpdateParam, comment);
        comment.setId(id);
        int rows = commentRepository.updateById(comment);
        if (rows != 1) {
            String message = "修改类别详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }




    @Override
    public List<CommentVO> listByComment() {
        log.debug("开始处理【查询评论的评论列表】的业务");
        return commentRepository.listByResourceType();
    }

    @Override
    public List<CommentVO> listBySelect() {
        log.debug("开始处理【查询评论的评论列表】的业务");
        return commentRepository.listBySelect();
    }

    @Override
    public List<CommentVO> listAnonymousSelect() {
        log.debug("开始处理【查询匿名评论的评论列表】的业务");
        return commentRepository.listAnonymousSelect();
    }


}
